package com.goddess.yeecolor.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;

import com.goddess.yeecolor.Modle.NewsModle;
import com.goddess.yeecolor.R;
import com.goddess.yeecolor.adapter.NewsActivityAdapter;
import com.goddess.yeecolor.base.BaseActivity;
import com.goddess.yeecolor.tools.JumpIntents;
import com.goddess.yeecolor.tools.MyUrl;
import com.goddess.yeecolor.tools.OkttpUtils;
import com.goddess.yeecolor.tools.SharePreceUtils;
import com.goddess.yeecolor.tools.ToastUtils;
import com.goddess.yeecolor.view.dialog_progress;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者: CaoJunping
 * 时间: 2017/1/13
 * 作用: ******
 * 备注: 消息页面
 */
public class NewsActivity extends BaseActivity {
    private LinearLayoutManager manager;
    private ImageView title_back;
    private XRecyclerView recyclerView;
    private NewsActivityAdapter adapter;
    private List<NewsModle> modleList=new ArrayList<>();
    private  int lomode;
    //记录分页 页数
    private int page=1;
    //记录上拉下拉状态
    private String PULL_STATE="";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);

        //数据初始化&控件初始化
        initFindView();
        initOnClck();
        initdatas();
    }
    public void initdatas(){
        PULL_STATE="STATE";
        setProgress();
        initData();
    }
    private dialog_progress dialog_progress;
    private void setProgress(){
        dialog_progress=new dialog_progress(this);
        dialog_progress.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog_progress.setCanceledOnTouchOutside(false);//按返回键可以关闭进度条
        dialog_progress.show();
    }

    @Override
    public void initData() {
        Map<Object,Object> map=new HashMap<>();
        map.put("unique", SharePreceUtils.getOther("user","unique"));
        map.put("page",page+"");
        OkttpUtils.getIntence().OkttpPost(MyUrl.NOTI,map,handler,1);
    }

    @Override
    public void initOnClck() {
        /**
         * 下拉刷新&下拉加载
         */
        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            /**
             * 下拉刷新
             */
            @Override
            public void onRefresh() {
                PULL_STATE="BOMOOT";
                page=1;
                if(adapter!=null&&modleList!=null){
                    adapter.clear();
                    adapter.notifyDataSetChanged();
                }
                initData();
            }
            /**
             * 上拉加载
             */
            @Override
            public void onLoadMore() {
                PULL_STATE="TOP";
                page++;
                initData();

            }
        });
        title_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setFiish(NewsActivity.this);
            }
        });


        /**
         *
         * item点击事件
         */

        adapter.setOnItemClicklistener(new NewsActivityAdapter.OnItemClicklistener() {
            @Override
            public void OnItemClick(View view, int position) {
                NewsModle modle=modleList.get(position);
                JumpIntents.startIntentCan(NewsActivity.this,NesWebViewActivity.class,modle.getWebview());
            }
        });

    }

    @Override
    public void initFindView() {
        //数据,控件初始化
        title_back= (ImageView) findViewById(R.id.title_back);
        recyclerView= (XRecyclerView)findViewById(R.id.news_activity_recyclerview);
        manager=new LinearLayoutManager(NewsActivity.this,LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(manager);
        recyclerView.setPullRefreshEnabled(false);
        /**
         * 设置上拉 下拉 样式
         */
        recyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        recyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallSpinFadeLoader);
        recyclerView.setArrowImageView(R.mipmap.ic_pulltorefresh_arrow);
        View view= LayoutInflater.from(NewsActivity.this).inflate(R.layout.empty,null);
        recyclerView.setEmptyView(view);
        adapter=new NewsActivityAdapter(NewsActivity.this,modleList);
    }
    private String jsonString;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            try {
                jsonString=msg.getData().getString("result");
                JSONObject object=new JSONObject(jsonString);
                int code=object.getInt("code");
                if (code==100){
                    if (object.has("result")){
                        JSONArray  result=object.getJSONArray("result");
                        for (int i=0;i<result.length();i++){
                            JSONObject data=result.getJSONObject(i);
                            NewsModle info=new NewsModle();
                            info.setTitle(data.getString("title"));
                            info.setId(data.getInt("id"));
                            info.setWebview(data.getString("webview"));
                            info.setDate(data.getString("addtime"));
                            info.setWord(data.getString("content"));
                            info.setUnque(data.getString("unique"));
                            modleList.add(info);
                        }
                    }
                    else {
                        JSONObject extend=object.getJSONObject("extends");
                        if (extend.getString("endfull").equals("0")){
                            ToastUtils.toast("暂无数据");
                        }
                    }
                }else {
                    ToastUtils.toast("暂无数据");
                }

                if(PULL_STATE.equals("BOMOOT")){
                    recyclerView.refreshComplete();
                }else if(PULL_STATE.equals("TOP")){
                    recyclerView.loadMoreComplete();
                }else{


                    if (dialog_progress.isShowing()){
                        dialog_progress.dismiss();
                    }
                    /**
                     * 适配器
                     */

                    recyclerView.setAdapter(adapter);
                    recyclerView.setRefreshing(true);
                }
                adapter.notifyDataSetChanged();
                recyclerView.setPullRefreshEnabled(true);
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dialog_progress!=null&&dialog_progress.isShowing()){
            dialog_progress.dismiss();
        }
    }
}
